package com.jy.api.controller.web;

import com.jy.api.domain.Result;
import com.jy.api.exception.MyException;
import com.jy.api.service.MemberService;
import com.jy.api.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lihu on 2017/12/6.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/web", produces = "application/json;charset=utf-8")
public class WebLoginController {

    @Autowired
    MemberService memberService;

    @Autowired
    WechatService wechatService;


    /**
     * 网页登陆授权
     *
     * @param openId
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/{openId}/member")
    public ResponseEntity login(@PathVariable String openId,
                                @RequestParam(name = "orderId") String orderId) {
        try {
            memberService.bindingOrder(openId, orderId);
            return ResponseEntity.ok(new Result(200));
        } catch (MyException ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity(ex.getMessage(), HttpStatus.BAD_REQUEST);
        } catch (Exception ex) {
            log.error("getMemberInfo is error==> orderId:{}", orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    /**
     * 微信推荐码
     *
     * @param storeId
     * @param orderId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/login")
    public ResponseEntity login1(@RequestParam("storeId") String storeId,
                                 @RequestParam("orderId") String orderId) {
        try {
            String url = wechatService.getLogUrl(storeId, orderId);
            log.info("login1:{}", url);
            return new ResponseEntity(url, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("login1 is error==> storeId:{},orderId:{}", storeId, orderId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
